﻿Imports Framework.Data

Public Class NetworkSettingsControl
    Implements Framework.Settings.ISettingsControl

    Public Function GetUserControl() As System.Windows.Forms.UserControl Implements Framework.Settings.ISettingsControl.GetUserControl
        Return Me
    End Function

    Public Sub LoadSettings() Implements Framework.Settings.ISettingsControl.LoadSettings
        txtHostName.Text = Framework.Net.Settings.Host
        txtPort.Text = Framework.Net.Settings.Port.ToString
        txtReconnectInterval.Text = Framework.Net.Settings.ReconnectInterval.ToString
        If Framework.Net.Settings.SetupLevel_Saved < 1 Then
            rbNetWorkMode_Public.Checked = True
        ElseIf Framework.Net.Settings.Listen Then
            rbNetWorkMode_Public.Checked = True
        Else
            rbNetWorkMode_Private.Checked = True
        End If
        chkAutoConnect.Checked = Framework.Net.Settings.AutoConnect
        chkTraceNetworkTraffic.Checked = Framework.Net.Settings.TraceTraffic
        chkThrottleDownstream.Checked = Framework.Net.Settings.ThrottleDownStream
        chkThrottleUpStream.Checked = Framework.Net.Settings.ThrottleUpStream
        nudDownStreamLimit.Value = CInt(Framework.Net.Settings.DownStreamBandwidthLimitBytesPerSecond / 1024)
        nudUpStreamLimit.Value = CInt(Framework.Net.Settings.UpStreamBandwidthLimitBytesPerSecond / 1024)
        nudDownStreamLimit.Enabled = chkThrottleDownstream.Checked
        nudUpStreamLimit.Enabled = chkThrottleUpStream.Checked
        chkUseDownloadMemoryBuffers.Checked = Framework.Net.Settings.UseReceiveDataMemoryBuffer
        chkUseUploadMemoryBuffers.Checked = False
    End Sub

    Public Function NeedsSave() As Boolean Implements Framework.Settings.ISettingsControl.NeedsSave
        If txtHostName.Text <> Framework.Net.Settings.Host Then Return True
        If txtPort.Text <> Framework.Net.Settings.Port.ToString Then Return True
        If txtReconnectInterval.Text <> Framework.Net.Settings.ReconnectInterval.ToString Then Return True
        If rbNetWorkMode_Private.Checked AndAlso Framework.Net.Settings.Listen Then Return True
        If rbNetWorkMode_Public.Checked AndAlso Not Framework.Net.Settings.Listen Then Return True
        If chkAutoConnect.Checked <> Framework.Net.Settings.AutoConnect Then Return True
        If chkTraceNetworkTraffic.Checked <> Framework.Net.Settings.TraceTraffic Then Return True
        If chkThrottleDownstream.Checked <> Framework.Net.Settings.ThrottleDownStream Then Return True
        If chkThrottleUpStream.Checked <> Framework.Net.Settings.ThrottleUpStream Then Return True
        If nudDownStreamLimit.Value * 1024 <> Framework.Net.Settings.DownStreamBandwidthLimitBytesPerSecond Then Return True
        If nudUpStreamLimit.Value * 1024 <> Framework.Net.Settings.UpStreamBandwidthLimitBytesPerSecond Then Return True
        If chkUseDownloadMemoryBuffers.Checked <> Framework.Net.Settings.UseReceiveDataMemoryBuffer Then Return True
        Return False
    End Function

    Private mItemsSaved As Boolean = False

    Public Sub SaveSettings(ByVal transactionScope As TransactionScope) Implements Framework.Settings.ISettingsControl.SaveSettings
        Framework.Net.Settings.SetListen(rbNetWorkMode_Public.Checked, transactionScope)
        If rbNetWorkMode_Public.Checked Then
            Framework.Net.Settings.SetHost(txtHostName.Text.Trim, transactionScope)
            Framework.Net.Settings.SetPort(Integer.Parse(txtPort.Text.Trim), transactionScope)
        End If
        Framework.Net.Settings.SetReconnectInterval(Integer.Parse(txtReconnectInterval.Text.Trim), transactionScope)
        Framework.Net.Settings.SetSetupLevel_Saved(Framework.Net.Settings.SetupLevel_Required, transactionScope)
        Framework.Net.Settings.SetAutoConnect(chkAutoConnect.Checked, transactionScope)
        Framework.Net.Settings.SetTraceTraffic(chkTraceNetworkTraffic.Checked, transactionScope)
        Framework.Net.Settings.SetThrottleDownStream(chkThrottleDownstream.Checked, transactionScope)
        Framework.Net.Settings.SetThrottleUpStream(chkThrottleUpStream.Checked, transactionScope)
        Framework.Net.Settings.SetDownStreamBandwidthLimitBytesPerSecond(CInt(nudDownStreamLimit.Value * 1024), transactionScope)
        Framework.Net.Settings.SetUpStreamBandwidthLimitBytesPerSecond(CInt(nudUpStreamLimit.Value * 1024), transactionScope)
        Framework.Net.Settings.SetUseReceiveDataMemoryBuffer(chkUseDownloadMemoryBuffers.Checked, transactionScope)
        transactionScope.SetInstanceFieldValue(Me, "mItemsSaved", True)
    End Sub

    Public Sub ValidateSettings(ByVal errorProvider As Framework.ErrorHandling.ErrorProviderEx) Implements Framework.Settings.ISettingsControl.ValidateSettings
        If rbNetWorkMode_Public.Checked Then
            If txtHostName.Text.Trim.Length = 0 Then
                errorProvider.AddError(txtHostName, "Invalid host name!")
            Else
                Try
                    Dim ip As System.Net.IPHostEntry = System.Net.Dns.GetHostEntry(txtHostName.Text)
                Catch ex As System.Net.Sockets.SocketException
                    errorProvider.AddError(txtHostName, ex.Message)
                End Try
            End If
            If txtPort.Text.Trim.Length = 0 Then
                errorProvider.AddError(txtPort, "Invalid port!")
            Else
                Dim iPort As Integer = 0
                If Not Integer.TryParse(txtPort.Text, iPort) Then
                    errorProvider.AddError(txtPort, "Invalid port!")
                Else
                    If iPort < 1 Or iPort > 65534 Then
                        errorProvider.AddError(txtPort, "Port must be between 1 and 65534!")
                    ElseIf iPort <> Framework.Net.Settings.Port Then
                        Try
                            Dim clsListen As New System.Net.Sockets.TcpListener(System.Net.IPAddress.Any, iPort)
                            clsListen.Start()
                            clsListen.Stop()
                        Catch ex As Exception
                            errorProvider.AddError(txtPort, ex.Message)
                        End Try
                    End If
                End If
            End If
        End If
        Dim iReconnectInterval As Integer = 0

        If Integer.TryParse(txtReconnectInterval.Text, iReconnectInterval) Then
            If iReconnectInterval < 300 Or iReconnectInterval > 30000 Then
                errorProvider.AddError(txtReconnectInterval, "Reconnect interval must be between 300 and 30000!")
            End If
        Else
            errorProvider.AddError(txtReconnectInterval, "Invalid reconnect interval!")
        End If
    End Sub

    Public Sub SaveCompleted() Implements Framework.Settings.ISettingsControl.SaveCompleted
        If mItemsSaved Then Manager.OnSettingsChanged()
        Manager.ReevalSettings()
    End Sub

    Public ReadOnly Property DisplayName() As String Implements Framework.Settings.ISettingsControl.DisplayName
        Get
            Return "Network"
        End Get
    End Property

    Public ReadOnly Property Key() As String Implements Framework.Settings.ISettingsControl.Key
        Get
            Return Settings.NetworkSettingsTabKey
        End Get
    End Property

    Private Sub chkThrottleDownstream_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkThrottleDownstream.CheckedChanged
        nudDownStreamLimit.Enabled = chkThrottleDownstream.Checked
    End Sub

    Private Sub chkThrottleUpStream_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkThrottleUpStream.CheckedChanged
        nudUpStreamLimit.Enabled = chkThrottleUpStream.Checked
    End Sub

    Public Sub AfterClose() Implements Framework.Settings.ISettingsControl.AfterClose

    End Sub

    Public Sub BeforeShow() Implements Framework.Settings.ISettingsControl.BeforeShow

    End Sub

    Public ReadOnly Property DisplayImage() As System.Drawing.Image Implements Framework.Settings.ISettingsControl.DisplayImage
        Get
            Return Framework.My.Resources.Network_16x16
        End Get
    End Property
End Class
